home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Disk / Directory / event.c < prev    next >
Encoding:
Text File  |  1993-02-03  |  7.3 KB  |  416 lines  |  [TEXT/KAHL]

  1. //-- EVENT.C
  2.  
  3. // Handling events that come up.
  4. #include <Events.h>
  5.  
  6. #include <stdio.h>
  7. #include "res.h"
  8. #include "struct.h"
  9.  
  10.  
  11. extern MenuHandle applMenu;
  12. extern MenuHandle fileMenu;
  13. extern MenuHandle editMenu;
  14. extern unsigned char ColorQD;
  15. extern unsigned char MultiWidget;
  16. static unsigned char QuitFlag;
  17.  
  18. //------------------------------------------------------------------------//                                                                                            */
  19. //----EVENTS-------How to handle the interface to the event stuff---------//                                */
  20. //------------------------------------------------------------------------//                                                                                            */
  21.  
  22. //-- GetEvent
  23.  
  24. // Based upon the flag settings in ModeFlags, this calls either GetNextEvent or WaitNextEvent
  25. // for the next event in the event queue.
  26.  
  27. int GetEvent(event)
  28. EventRecord *event;
  29. {
  30.     if (MultiWidget) {
  31.         return WaitNextEvent(everyEvent,event,30L,NULL);        /* Allow timeout for 1/2 second */
  32.     } else {
  33.         SystemTask();
  34.         return GetNextEvent(everyEvent,event);
  35.     }
  36. }
  37.  
  38. //-- DoEvent
  39.  
  40. // This fetches the next event from the event queue, dispatching the event as needed.
  41.  
  42. int DoEvent()
  43. {
  44.     EventRecord event;
  45.     char c;
  46.     short wlow,whigh;
  47.     Point p;
  48.     char buffer[64];
  49.     long l;
  50.     short s;
  51.  
  52.     QuitFlag = 0;
  53.     GetEvent(&event);
  54.     switch (event.what) {
  55.         case nullEvent:
  56.             SearchDisks();
  57.             break;
  58.         case mouseDown:
  59.             DoMouse(&event);
  60.             break;
  61.         case keyDown:
  62.         case autoKey:
  63.             c = event.message & charCodeMask;
  64.             if (event.modifiers & cmdKey) {
  65.                 if (event.what == keyDown) {
  66.                     DoMenu(MenuKey(c));
  67.                 }
  68.             } else DoKey(c,FrontWindow());
  69.             break;
  70.         case updateEvt:
  71.             DoUpdate(event.message);
  72.             break;
  73.         case activateEvt:
  74.             DoActivate(event.message,(event.modifiers & activeFlag) ? 1 : 0);
  75.             break;
  76.         case diskEvt:
  77.             wlow = (event.message) & 0x0FFFF;
  78.             whigh = (event.message) >> 16;
  79.             if (whigh != 0) {
  80.                 DILoad();                        /* Prepare to init disk */
  81.                 p.h = p.v = 75;
  82.                 whigh = DIBadMount(p, event.message);
  83.                 DIUnload();
  84.                 if (whigh != 0) break;
  85.             }
  86.             GetVInfo(wlow,buffer,&s,&l);
  87.             NewPlan(s);
  88.             break;
  89.     }
  90.  
  91.     return QuitFlag;
  92. }
  93.  
  94. //-- DoMouse
  95.  
  96. // What to do when the mouse goes down somewhere in the application.
  97.  
  98. DoMouse(event)
  99. EventRecord *event;
  100. {
  101.     int i;
  102.     WindowPtr w;
  103.     GrafPtr p;
  104.     Rect r;
  105.     long l;
  106.  
  107.     i = FindWindow(event->where,&w);
  108.     if (w != NULL) SetPort(w);
  109.  
  110.     switch (i) {
  111.         case inDesk:
  112.             break;
  113.         case inMenuBar:
  114.             DoMenu(MenuSelect(event->where));
  115.             break;
  116.         case inSysWindow:
  117.             SystemClick(event,w);
  118.             break;
  119.         case inGrow:
  120.             r.top = 100;
  121.             r.left = 150;
  122.             r.bottom = 32767;
  123.             r.right = 32767;            /* Absolutely rediculous size */
  124.             l = GrowWindow(w,event->where,&r);
  125.             if (l != 0) {
  126.                 DoResizeInit(w);
  127.                 EraseRect(&(w->portRect));
  128.                 SizeWindow(w,(short)(l & 0x0FFFF),(short)(l >> 16),false);
  129.                 DoResizeWindow(w);
  130.                 InvalRect(&(w->portRect));
  131.             }
  132.             break;
  133.         case inContent:
  134.             if (w != FrontWindow()) SelectWindow(w);
  135.             else DoWindowClick(event,w);
  136.             break;
  137.         case inDrag:
  138.             GetWMgrPort(&p);
  139.             r = p->portRect;
  140.             r.top += 38;
  141.             InsetRect(&r,4,4);
  142.             DragWindow(w,event->where,&r);
  143.             break;
  144.         case inGoAway:
  145.             if (TrackGoAway(w,event->where)) CloseWin(w);
  146.             break;
  147.         case inZoomIn:
  148.         case inZoomOut:
  149.             if (TrackBox(w,event->where,i)) {
  150.                 DoResizeInit(w);
  151.                 EraseRect(&(w->portRect));
  152.                 ZoomWindow(w,i,false);
  153.                 DoResizeWindow(w);
  154.                 InvalRect(&(w->portRect));
  155.             }
  156.             break;
  157.     }
  158. }
  159.  
  160. //------------------------------------------------------------------------//                                                                                            */
  161. //-----------MENUS-----How to handle the interface to the menu bar--------//
  162. //------------------------------------------------------------------------//                                                                                            */
  163.  
  164. //-- DoMenu
  165.  
  166. // What to do when something happens in the menu bar.
  167.  
  168. DoMenu(l)
  169. long l;
  170. {
  171.     short hi,lo;
  172.     short i;
  173.  
  174.     hi = (short)(l >> 16);
  175.     lo = (short)(l & 0x0FFFF);
  176.  
  177.     switch (hi) {
  178.         case APPLMENU:
  179.             if (lo == ABOUTMEMENU) DoAboutMe();
  180.             else {
  181.                 char buffer[64];
  182.                 GetItem(applMenu,lo,buffer);
  183.                 OpenDeskAcc(buffer);
  184.             }
  185.             break;
  186.         case FILEMENU:
  187.             switch (lo) {
  188.                 case CLOSEMENU:
  189.                     CloseWin(FrontWindow());
  190.                     break;
  191.                 case QUITMENU:
  192.                     QuitFlag = 1;
  193.                     return;
  194.                 case SAVEMENU:
  195.                     SaveText();
  196.                     break;
  197.                 case PRINTSTL:
  198.                     PrintStl();
  199.                     break;
  200.                 case PRINTJOB:
  201.                     PrintJob();
  202.                     break;
  203.             }
  204.             break;
  205.         case EDITMENU:
  206.             if (!SystemEdit(lo-1)) switch (lo) {
  207.                 case OPTIONS:
  208.                     OptionDialog();
  209.                     break;
  210.             }
  211.             break;
  212.     }
  213.     HiliteMenu(0);
  214. }
  215.  
  216.  
  217. //------------------------------------------------------------------------//                                                                                            */
  218. //-----------------WINDOWS---------How to handle windows------------------//
  219. //------------------------------------------------------------------------//                                                                                            */
  220.  
  221. //-- CloseWin
  222.  
  223. // This closes a window.  Return TRUE if the window was closed successfully.
  224.  
  225. int CloseWin(w)
  226. WindowPtr w;
  227. {
  228.     short i;
  229.  
  230.     if (w == NULL) return true;            /* No window to close */
  231.     i = ((WindowPeek)w)->windowKind;    /* Figure out what window we have and close it */
  232.     if (i < 0) {
  233.         CloseDeskAcc(i);                /* Shut down the desk accessory */
  234.         return true;
  235.     } else switch (i) {                    /*CH*/
  236.         case WK_PLAN:
  237.             return ClosePlan(w);
  238.         default:
  239.             DisposeWindow(w);
  240.             return true;
  241.     }
  242. }
  243.  
  244.  
  245. //-- DoWindowClick
  246.  
  247. // This figures out what to do when the mouse goes down.
  248.  
  249. DoWindowClick(e,w)
  250. EventRecord *e;
  251. WindowPtr w;
  252. {
  253.     short i;
  254.     Point p;
  255.     ControlHandle c;
  256.     short j,k;
  257.     Rect r;
  258.     Rect s;
  259.     WindowPtr port;
  260.  
  261.     if (w == NULL) return;
  262.     i = ((WindowPeek)w)->windowKind;
  263.     p = e->where;
  264.     GlobalToLocal(&p);
  265.     switch (i) {                        /*CH*/
  266.         case WK_PLAN:
  267.             MousePlan(w,e,&p);
  268.             break;
  269.     }
  270. }
  271.  
  272.  
  273. //-- DoResizeInit
  274.  
  275. // Initialize any variables which need to be initialized before resizing a window.
  276.  
  277. DoResizeInit(w)
  278. WindowPtr w;
  279. {
  280.     short i;
  281.  
  282.     if (w == NULL) return;
  283.     i = ((WindowPeek)w)->windowKind;
  284.     switch (i) {                        /*CH*/
  285.         case WK_PLAN:
  286.             ResizeInitPlan(w);
  287.             break;
  288.     }
  289. }
  290.  
  291.  
  292. //-- DoResizeWindow
  293.  
  294. // This resizes the data structures for a new size window.
  295.  
  296. DoResizeWindow(w)
  297. WindowPtr w;
  298. {
  299.     short i;
  300.  
  301.     if (w == NULL) return;
  302.     i = ((WindowPeek)w)->windowKind;
  303.     switch (i) {                        /*CH*/
  304.         case WK_PLAN:
  305.             ResizePlan(w);
  306.             break;
  307.     }
  308. }
  309.  
  310.  
  311. //-- DoKey
  312.  
  313. // What to do when the user hits a key.
  314.  
  315. DoKey(c,w)
  316. char c;
  317. WindowPtr w;
  318. {
  319.     short i;
  320.  
  321.     if (w == NULL) return;
  322.     i = ((WindowPeek)w)->windowKind;
  323.     switch (i) {
  324.     }
  325. }
  326.  
  327.  
  328. //-- DoUpdate
  329.  
  330. // This does the window update.
  331.  
  332. DoUpdate(w)
  333. WindowPtr w;
  334. {
  335.     short i;
  336.  
  337.     if (w == NULL) return;
  338.     i = ((WindowPeek)w)->windowKind;
  339.  
  340.     BeginUpdate(w);
  341.     SetPort(w);
  342.     switch (i) {                        /*CH*/
  343.         case WK_PLAN:
  344.             UpdatePlan(w);
  345.             break;
  346.     }
  347.     EndUpdate(w);
  348. }
  349.  
  350.  
  351. //-- DoActivate
  352.  
  353. // Handle the activate event.
  354.  
  355. DoActivate(w,flag)
  356. WindowPtr w;
  357. int flag;
  358. {
  359.     short i;
  360.  
  361.     if (w == NULL) return;
  362.     i = ((WindowPeek)w)->windowKind;
  363.     switch (i) {                        /*CH*/
  364.         case WK_PLAN:
  365.             ActivatePlan(w,flag);
  366.             break;
  367.     }
  368. }
  369.  
  370.  
  371.  
  372. //-- AboutMeDH
  373.  
  374. // The function to pass to ModalDialog for the 'about me' dialog.
  375.  
  376. pascal Boolean AboutMeDH(theDialog,theEvent,itemHit)
  377. DialogPtr *theDialog;
  378. EventRecord *theEvent;
  379. short *itemHit;
  380. {
  381.     if (theEvent->what == mouseDown) {
  382.         *itemHit = 1;
  383.         return true;
  384.     } else return false;
  385. }
  386.  
  387.  
  388.  
  389.  
  390. //-- DoAboutMe
  391.  
  392. // How to handle the 'about me' box.
  393.  
  394. DoAboutMe()
  395. {
  396.     DialogPtr dlog;
  397.     short i1;
  398.     Handle i2;
  399.     Rect i3;
  400.     short x,y;
  401.     char **ptr;
  402.     char buffer[32];
  403.  
  404.     dlog = GetNewDialog(ABOUTMEDLOG,NULL,(char *)-1);
  405.     if (dlog == NULL) return;
  406.     SetPort(dlog);
  407.  
  408.     for (;;) {
  409.         ModalDialog(AboutMeDH,&i1);
  410.         if (i1 == 1) break;
  411.     }
  412.  
  413.     DisposDialog(dlog);
  414. }
  415.  
  416.